import AudioManager from "./AudioManager";
import PeripheralSystemDataMgr from "./PeripheralSystemDataMgr";
import EngineUtil from "./EngineUtil";
import BasePageCtrl, { AnimType } from "./BasePageCtrl";
import SkillRewardPage from "./SkillRewardPage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SkillRewardPageCtrl extends BasePageCtrl {
    ui: SkillRewardPage = null;

    onLoad() {
        this.onUILoad();
        super.onLoad();
        this._animType = AnimType.SCALE;
        this._touchControl = false;
        this._hasPeneLock = true;
        this._hasBlack = false;
        this._hasTouchLock = false;
        this._hasBlackTouch = false;
        this._reuse = false;
        this.addButtonListen();
        this.initUI();
    }

    start() { }

    initUI() {
        let positions = [];
        let elements = [this.ui.hammer, this.ui.bowAndArrow, this.ui.artillery];
        elements.forEach((element) => {
            positions.push(element.getPosition());
        });
        positions = EngineUtil.shuffle(positions);
        elements = EngineUtil.shuffle(elements);
        this.playShowAction(elements, positions);
    }

    playShowAction(elements, positions) {
        for (let i = 0; i < elements.length; i++) {
            const element = elements[i];
            const position = positions[i];
            element.setPosition(position);
            element.opacity = 0;
            element.scale = 0;
            cc.tween(element)
                .delay(0.15 * i)
                .to(0.15, { opacity: 255, scale: 0.7 })
                .call(() => {
                    AudioManager.getInstance().playMusic("booster_collect", false, false, null, true);
                })
                .delay(0.15 * i)
                .call(() => {
                    if (i === elements.length - 1) {
                        this.playFlyAction(elements);
                    }
                })
                .start();
        }
    }

    playFlyAction(elements) {
        const tagNode = PeripheralSystemDataMgr.getTagNodeByType(2);

        for (let i = 0; i < elements.length; i++) {
            const element = elements[i];
            const targetPos = EngineUtil.convertNodePosition(element, tagNode);

            cc.tween(element)
                .delay(0.15 * i)
                .to(0.7, { x: targetPos.x, y: targetPos.y + 30 }, { easing: "backIn" })
                .call(() => {
                    AudioManager.getInstance().playMusic("booster_appear", false, false, null, true);
                    element.active = false;
                    this.playTagNodeAction();

                    if (i === elements.length - 1) {
                        PeripheralSystemDataMgr.checkNextOutSideGuide();
                        PeripheralSystemDataMgr.checkShowGameStartPage();
                        this.hide();
                    }
                })
                .start();
        }
    }

    playTagNodeAction() {
        const tagNode = PeripheralSystemDataMgr.getTagNodeByType(2);

        cc.tween(tagNode)
            .to(0.05, { scale: 1.1 })
            .to(0.05, { scale: 1 })
            .call(() => {
                tagNode.scale = 1;
            })
            .start();
    }

    onUILoad() {
        this.ui = this.node.addComponent(SkillRewardPage);
    }

    addButtonListen() { }

    clickClose() {
        this.hide();
    }
}
